#coding=utf8

import urllib
import urllib2
import httplib
import cookielib

class loginPageGeter:
    
    httpcookie = ''

    #构造函数
    def __init__(self):
        cookie = cookielib.CookieJar()
        self.httpcookie = urllib2.HTTPCookieProcessor(cookie)
    
    #登录函数
    def login(self, url, params):
        params = urllib.urlencode(params)
        req = urllib2.Request(url, params)
        opener = urllib2.build_opener(self.httpcookie)
        fp = opener.open(req)
        print fp.read()
        self.get()
        return fp.read()

    #GET函数测试
    def get(self):
        opener=urllib2.build_opener(self.httpcookie)
        fp=opener.open('http://183.129.135.196:8080/Admin_Menu.asp?type=1&DBFlag=0')
        print fp.read()
        fl=open('jieguo.txt','w+')
        jieguo=fp.read()
        fl.write(jieguo)
        fl.close()
    # #post数据
    # def post(self, url, params):
    #     try:
    #         params = urllib.urlencode(params)
    #         req = urllib2.Request(url, params)
    #         opener = urllib2.build_opener(self.httpcookie)
    #         fp = opener.open(req)
    #         return fp.read()
    #     except:
    #         return False

if __name__=='__main__':
    loginObj = loginPageGeter()

    #登录
    print '正在登录...'
    url = 'http://183.129.135.196:8080/Check_Admin.asp?Type=1'
    params = {'Cs_Admin_Flag':'2',
'Cs_Admin_Name':'201115148',
'Cs_Admin_Pass':'qwe2072135'}
    loginObj.login(url, params)
    
    #给用户发10条消息
    # for i in range(1, 11):
    #     print ('%s\t正在发送消息...' % i).decode('utf-8')
    #     url = 'http://xuxb.v2.local/member/send_message.php'
    #     params = {'friendId':'1790251', 'message':'机器自动发送', 'act':'add'}
    #     loginObj.post(url, params) 